﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using System.Xml.Linq;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;

public partial class reports_print_profile : System.Web.UI.Page
{
    ReportDocument rptDoc = new ReportDocument();
    ParameterField paramField = new ParameterField();
    ParameterFields paramFields = new ParameterFields();
    ParameterDiscreteValue paramDiscretevalues = new ParameterDiscreteValue();
    ParameterValues paramValues = new ParameterValues();
    private const string PARAMETER_FIELD_NAME = "profile_id";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["username"] != null&&Request.Params["id"] != null)
            LoadReport();
        else Response.Redirect("../login/login.aspx");
    }

    private void LoadReport()
    {

        if (Request.Params["id"] != null)
        {
            paramField.Name = "@profile_id";
            //paramDiscretevalues.Value = p.ID.ToString();
            paramDiscretevalues.Value = Request.QueryString["id"].ToString();
            paramField.CurrentValues.Add(paramDiscretevalues);
            paramFields.Add(paramField);
            rptViewProfile.ParameterFieldInfo = paramFields;

            rptDoc.Load(Server.MapPath("~/reports/rpt_profile.rpt"));


            Common.SetConnectionInfo(rptDoc.Database.Tables);
            rptViewProfile.PrintMode = CrystalDecisions.Web.PrintMode.ActiveX;
            rptViewProfile.ReportSource = rptDoc;
            Session["report"] = rptDoc;
            //Session.Add(
        }
        else
        {
            Response.Redirect(Server.MapPath("~/profiles/list_profiles.aspx"));
        }

        //return rptDoc;
    }

    protected override void OnUnload(EventArgs e)
    {

        base.OnUnload(e);
        this.Unload += new EventHandler(Page_Unload);

    }
    public void Page_Unload(object sender, EventArgs e)
    {

        // clean up the resources

        rptDoc.Close();
        rptDoc.Dispose();
        GC.Collect();


    }

    protected void btnPrint_Click(object sender, EventArgs e)
    {
        if (Request.Params["id"] != null)
        {
            paramField.Name = "@profile_id";

            paramDiscretevalues.Value = Request.QueryString["id"].ToString();
            paramField.CurrentValues.Add(paramDiscretevalues);
            paramFields.Add(paramField);
            rptViewProfile.ParameterFieldInfo = paramFields;
            rptViewProfile.PrintMode = CrystalDecisions.Web.PrintMode.ActiveX;
            rptDoc.Load(Server.MapPath("~/reports/rpt_profile.rpt"));
            Common.SetConnectionInfo(rptDoc.Database.Tables);
           
            rptDoc.SetParameterValue("@profile_id", Request.QueryString["id"].ToString());
            rptDoc.PrintOptions.PrinterName = "HP LaserJet P2035n";
            rptDoc.PrintToPrinter(1, false, 1, 1);
            
            // rptDoc.ExportToDisk(ExportFormatType.PortableDocFormat, Server.MapPath("~/reports/p.pdf"));
        }
    }
    protected void btnExport_Click(object sender, EventArgs e)
    {
        if (Request.Params["id"] != null)
        {
            
            rptDoc.Load(Server.MapPath("~/reports/rpt_profile.rpt"));
            Common.SetConnectionInfo(rptDoc.Database.Tables);
            rptDoc.SetParameterValue("@profile_id", Request.QueryString["id"].ToString());
            rptDoc.ExportToDisk(ExportFormatType.PortableDocFormat, Server.MapPath("~/reports/profile.pdf"));
            ClientScript.RegisterStartupScript(this.Page.GetType(), "popupOpener", "var popup=window.open('/hr/reports/profile.pdf');popup.focus();", true);
            //ClientScript.RegisterStartupScript(this.Page.GetType(), "popupOpener", "var popup=window.open('Files/BiWeeklyReport.pdf');popup.focus();", true);



        }

    }
    protected void lnkLogout_Click(object sender, EventArgs e)
    {
        FormsAuthentication.SignOut();
       
        Session.Clear();
        Response.Redirect("../login/login.aspx");
    }
}
